﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MiniFox.Infrastructure
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public sealed class ViewRowCollection : IList<ViewRow>
    {
        ViewTable _viewTable;
        internal ViewRowCollection(ViewTable viewTable)
        {
            _viewTable = viewTable;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int IndexOf(ViewRow row)
        {
            return _viewTable.RowList.IndexOf(row);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="row"></param>
        public void Insert(int rowIndex, ViewRow row)
        {
            throw new NotSupportedException();
            //if (Exists(row))
            //    return;
            //_viewTable.RowList.Insert(rowIndex, row);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        public void Add(ViewRow row)
        {
            if (Exists(row))
                return;
            //row.RowIndex = _viewTable.RowList.Count;
            _viewTable.RowList.Add(row);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private bool Exists(ViewRow row)
        {
            bool exists = false;
            var primarykeys = _viewTable.Columns.Where(p => p.IsKey);
            
            if (primarykeys.Count() > 0)
                foreach (ViewRow viewRow in _viewTable.RowList)
                {
                    bool isMatched = true;
                    foreach (ViewColumn col in primarykeys)
                    {
                        isMatched &= row[col.Name] == viewRow[col.Name];
                        if (!isMatched) //未匹配主键时，退出循环
                            break;
                    }
                    if (isMatched) //有完全匹配的主键时，退出循环
                    {
                        exists = true;
                        break;
                    }
                }
            return exists;
        }
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="rowIndex"></param>
        public void RemoveAt(int rowIndex)
        {
            _viewTable.RowList.RemoveAt(rowIndex);
        }
        /// <summary>
        /// 行号索引提取
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public ViewRow this[int rowIndex]
        {
            get
            {
                return _viewTable.RowList[rowIndex];
            }
            set
            {
                _viewTable.RowList[rowIndex] = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            _viewTable.RowList.Clear();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(ViewRow item)
        {
            return _viewTable.RowList.Contains(item);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(ViewRow[] array, int arrayIndex)
        {
            _viewTable.RowList.CopyTo(array, arrayIndex);
        }
        /// <summary>
        /// 
        /// </summary>
        public int Count
        {
            get { return _viewTable.RowList.Count; }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool IsReadOnly
        {
            get { return false; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(ViewRow item)
        {
            return _viewTable.RowList.Remove(item);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<ViewRow> GetEnumerator()
        {
            return _viewTable.RowList.GetEnumerator();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
